package timit;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: lahiru
 * Date: 6/11/14
 * Time: 5:55 PM
 * To change this template use File | Settings | File Templates.
 */
public class CalculateTransformDistances {
    public static void main(String[] args) throws IOException{
        String file = "/home/lahiru/experiments/Speaker-effects/vectors/transformLayer1/pdfVectors_layer1_to_5.ark";
        int vectorCount = 117;
        int numberOfDimentions = 1024;
        double totalDistance = 0.0;

        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        boolean firstLine = true;
        List<double[]> vectors = new ArrayList<double[]>();

        while ((line = br.readLine()) != null) {
          if (firstLine){    //ommit the first line
              firstLine=false;
          }else{
              String hArray[] = line.trim().split(" ");
              double vector[] = new double[numberOfDimentions];
              for (int i = 0; i < numberOfDimentions; i++) {
                  vector[i] = Double.parseDouble(hArray[i].trim());
              }
              vectors.add(vector);
          }
        }
        br.close();

        vectorCount= vectors.size();
        System.out.println("The vector counts is " + vectorCount);

        for(int i=0; i < vectorCount; i++){
            for(int j=0; j<vectorCount; j++){
                double distance = CalculatePhonemeStats.calculateKLDivergenceVectors(vectors.get(i), vectors.get(j));
                totalDistance = totalDistance + distance;
            }
        }

        System.out.println("The total distance " + totalDistance);
        System.out.println("The average distance " + totalDistance/vectorCount);

    }
}
